package org.example.controller;

import lombok.extern.slf4j.Slf4j;
import org.example.generator.DataGenerator;
import org.example.service.DataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * 大规模数据生成控制器
 */
@Slf4j
@RestController
@RequestMapping("/large-scale-data")
public class LargeScaleDataController {

    @Autowired
    private DataGenerator dataGenerator;
    @Autowired
    private DataService dataService;

    /**
     * 启动大规模数据生成
     */
    @PostMapping("/generate")
    public ResponseEntity<Map<String, Object>> generateLargeScaleData() {
        Map<String, Object> response = new HashMap<>();
        
        try {
            log.info("开始大规模数据生成（目标：50亿放款，20亿在途余额）");
            
            long startTime = System.currentTimeMillis();
            dataService.clearExistingData();
            
            // 调用批量插入方法
            DataGenerator.DataGenerationResult result = dataGenerator.generateCompleteDataSetWithBatchInsert();
            
            long endTime = System.currentTimeMillis();
            long duration = endTime - startTime;
            
            response.put("success", true);
            response.put("message", "大规模数据生成完成");
            response.put("duration", duration + " 毫秒");
            response.put("durationSeconds", duration / 1000);
            response.put("totalOrders", result.getOrders().size());
            response.put("totalCustomers", result.getCustomers().size());
            response.put("totalRepayPlans", result.getRepayPlans().size());
            
            log.info("大规模数据生成完成，耗时: {} 秒", duration / 1000);
            
            return ResponseEntity.ok(response);
            
        } catch (Exception e) {
            log.error("大规模数据生成失败", e);
            response.put("success", false);
            response.put("message", "数据生成失败: " + e.getMessage());
            response.put("error", e.getClass().getSimpleName());
            
            return ResponseEntity.internalServerError().body(response);
        }
    }

    /**
     * 获取数据生成状态
     */
    @GetMapping("/status")
    public ResponseEntity<Map<String, Object>> getGenerationStatus() {
        Map<String, Object> response = new HashMap<>();
        
        response.put("status", "ready");
        response.put("targetLoanAmount", "50亿");
        response.put("targetOutstandingAmount", "20亿");
        response.put("batchSize", "2000条/批");
        response.put("description", "大规模数据生成服务已准备就绪");
        
        return ResponseEntity.ok(response);
    }

    /**
     * 清理数据库（可选功能）
     */
    @DeleteMapping("/cleanup")
    public ResponseEntity<Map<String, Object>> cleanupData() {
        Map<String, Object> response = new HashMap<>();
        
        try {
            // 这里可以添加清理逻辑
            log.info("数据清理功能暂未实现");
            
            response.put("success", true);
            response.put("message", "数据清理功能暂未实现");
            
            return ResponseEntity.ok(response);
            
        } catch (Exception e) {
            log.error("数据清理失败", e);
            response.put("success", false);
            response.put("message", "数据清理失败: " + e.getMessage());
            
            return ResponseEntity.internalServerError().body(response);
        }
    }
}




